package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.OrderSpecification;
import dbHelper.DBHelper;
import entities.Order;

public class OrderRepository implements Repository<Order>{
	
	public ArrayList<Order> OrderList;
	
	
	public OrderRepository()
	{
		this.OrderList = null;
	}

	@Override
	public void Add(Order Order) {
		try {
			DBHelper.addObject(Order);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Order Order) {
		try {
			DBHelper.deleteObject(Order);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(Order Order) {
		try {
			DBHelper.updateObject(Order);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		Order O = new Order();
		try {
			this.OrderList = (ArrayList<Order>) DBHelper.findObjects(O);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Order> findBySpecification (OrderSpecification spec) {
		if(this.OrderList == null)
			this.FindAll();
		ArrayList<Order> res = new ArrayList<Order>();
		for (Order el : OrderList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}

}
